/*
 * Joystick.cpp
 *
 * Author: Samuel Queiroz
 */

#include "Joystick.h"

void Joystick::run()
{
	if (event->type == SDL_JOYAXISMOTION)
	{
		leftButton->setX(getVirtualValueOf(*leftXAxis, true));
		leftButton->setY(getVirtualValueOf(*leftYAxis, false));
		rightButton->setX(getVirtualValueOf(*rightXAxis, true));
		rightButton->setY(getVirtualValueOf(*rightYAxis, false));
	}
}

int Joystick::getVirtualValueOf(int realValue, bool xAxis)
{
	int virtualValue = abs(realValue) - (MAXIMUM_VALUE * ZERO_PERCENTAGE);

	if (virtualValue < 0)
	{
		return 0;
	}

	int maxVirtualValue = xAxis ? xMaxVirtualValue : yMaxVirtualValue;
	int virtualUnitRange = xAxis ? xVirtualUnitRange : yVirtualUnitRange;

	int resultValue = ((abs(virtualValue) >= maxVirtualValue ? maxVirtualValue - 1 : abs(virtualValue))
			/ virtualUnitRange) + 1;

	return (realValue < 0) ? -resultValue : resultValue;
}
